package homework;
//作业标题(2741)
//另一棵树的子树
//
//作业内容
//另一棵树的子树
//https://leetcode.cn/problems/subtree-of-another-tree/description/
public class h2741
{
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null || subRoot == null) return false;
        return _isSubtree(root,subRoot) || _isSubtree(root.left, subRoot) || _isSubtree(root.right, subRoot);
    }
    private boolean _isSubtree(TreeNode root, TreeNode subRoot)
    {
        if(root == null && subRoot == null) return true;
        if(root == null && subRoot != null || root != null && subRoot == null) return false;
        return root.val == subRoot.val && _isSubtree(root.left, subRoot.left) && _isSubtree(root.right, subRoot.right);
    }
}
